package com.qianyitian.hope2.spider.fetcher;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qianyitian.hope2.spider.external.DanjuanClient;

import com.qianyitian.hope2.spider.model.Fund;
import com.qianyitian.hope2.spider.model.FundFinancialReport;
import com.qianyitian.hope2.spider.model.Stock;
import com.qianyitian.hope2.spider.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;


@Component("danjuanFundsRetreiver")
public class DanjuanFundsRetreiver extends WebStockRetreiver {

    @Autowired
    private DanjuanClient danjuanClient;

    public String getFundsHistory(String code) {
        String content = danjuanClient.getFundHistory(code);
        return content;
    }

    public FundFinancialReport getFinancialReport(String code) {
        String fundContent = danjuanClient.getFund(code);
        JSONObject fundObject = JSON.parseObject(fundContent);
        fundObject = fundObject.getJSONObject("data");

        String detailContent = danjuanClient.getFundDetail(code);
        Fund fundDetail = JSON.parseObject(detailContent, Fund.class);
        {
            FundFinancialReport report = new FundFinancialReport();
            report.setStockList(fundDetail.getData().getFundPosition().getStockList());
            report.setStockPercent(fundDetail.getData().getFundPosition().getStockPercent());
            report.setAssetValue(fundDetail.getData().getFundPosition().getAssetVal());
            report.setCode(fundObject.getString("fd_code"));
            report.setName(fundObject.getString("fd_name"));
            report.setMorningStar(fundObject.getString("rating_desc"));
            LocalDate date = DateUtil.date2LocalDate(fundDetail.getData().getFundPosition().getEnddate());
            report.setUpdateDate(date);
            return report;
        }
    }

    @Override
    public Stock fetchStockInfo(Stock code) throws IOException {
        throw new RuntimeException("not implemented");
    }

    @Override
    public Stock fetchIndexInfo(Stock code) throws IOException {
        throw new RuntimeException("not implemented");
    }

    @Override
    public String fetchFundsInfo(String code) throws IOException {
        String content = danjuanClient.getFundDetail(code);
        return content;
    }
}
